package ru.study.thinkin.chapter13;

import java.util.ArrayList;
import java.util.Arrays;

public class Staff  extends ArrayList<Position>{
	
	public Staff (String[] titles) {
		add(titles);
	}
	public void add(String title,Person person){
		add(new Position(title,person));
	}
	
	public void add(String[] titles) {
		for(String title:titles) {
			add(new Position(title));
		}
	}
	
	public boolean positionAvailable(String title){
		boolean answer = false;
		boolean found = false;
		
		for(Position position : this) {
			if(title.equalsIgnoreCase(position.getTitle())) {
				found =true;
				if(position.getPerson()==Person.NULL) {
					answer = true;					
				}
			}			
		}
		if (!found) {
			throw new RuntimeException("Position " + title + " not found");
		}
		
		return answer;
	}
	public void fillPosition(String title,Person hire) {		
		for	(Position position : this) {
		
		 	if (title.equalsIgnoreCase(position.getTitle()) && positionAvailable(title)){
		 		position.setPerson(hire);
		 		return;
			}				
		}	
		throw new RuntimeException("Position " + title + " not available");
	}
	
	public static void main(String[] args) {
		Staff staff = new Staff( new String[]{"President","CTO","Project Lead","Softfware Engineer"});
		System.out.println(staff);
		
		staff.fillPosition("President", new Person("P","V","V"));
		staff.fillPosition("CTO", new Person("S","A","v"));
		//staff.fillPosition("President", new Person("S","A","v"));
		
		System.out.println(staff);
	}

}
